<?php 

function list_jalur_karir(){
	$header = array(
			'No Organisasi'=>array('style'=>'border-bottom:2px solid;width:15%'),
			'Jabatan'=>array('style'=>'border-bottom:2px solid;width:40%'),
			'Job Family'=>array('style'=>'border-bottom:2px solid;width:35%'),
			' '=>array('style'=>'border-bottom:2px solid;width:10%'),
		);
	$com = isset($_GET['com']) ? $_GET['com']: '';
	$task = isset($_GET['task']) ? $_GET['task']: '';
	$field = isset($_GET['field']) ? $_GET['field']: '';
	$key = isset($_GET['key']) ? $_GET['key']: '';
	$query = "SELECT a.*,c.keluarga_jabatan_label 
		FROM jabatan a	
		LEFT JOIN jalur_karir b ON a.jabatan_id = b.jabatan_id  
		LEFT JOIN keluarga_jabatan c ON c.keluarga_jabatan_id = b.keluarga_jabatan_id  		
		INNER JOIN sk_jabatan d ON d.sk_id=a.sk_id
		WHERE d.active='Y' 
		ORDER BY a.urut" ;		

	$result = my_query($query );
	$total_records = my_num_rows($result );
	$scroll_page =5;  
	$per_page =15;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com={$com}&task={$task}&field={$field}&key={$key}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	$pager_url_last = null;
	$kgPagerOBJ = new kgPager();
			$kgPagerOBJ->pager_set(
				$pager_url, 
				$total_records, 
				$scroll_page, 
				$per_page, 
				$current_page, 
				$inactive_page_tag, 
				$previous_page_text, 
				$next_page_text, 
				$first_page_text, 
				$last_page_text ,$pager_url_last); 
	 		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page);  
	
	$i = ($current_page  - 1 ) * 35; 
	$row = array();

	while($ey  = my_fetch_array($result) ){
		
		$editproperty = array(
			'href'=>'index.php?com=jalur_karir&task=edit_popup&id='.$ey['jabatan_id'],
			'rel'=>'facebox',
			'title'=>'Job Family'
		);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );

		$detailproperty = array(
			'href'=>'index.php?com=jalur_karir&task=detail&id='.$ey['jabatan_id'],
			'title'=>'Carrer Path'
		);
		$detail_button = button_icon( 'b_props.png' , $detailproperty  );

		$row[] = array(
			'no_posisi'=> $ey['nomor_posisi'],
			'jabatan'=>$ey['nama_jabatan'],	
			'kejabatan'=>$ey['keluarga_jabatan_label'],
			'operasi'=> position_text_align(  $edit_button.' '.$detail_button , 'right'), 
			);
	}
	$datas = table_rows($row);  
	$paging = $kgPagerOBJ ->showPaging();
	return table_builder($header , $datas ,  4 , false ,$paging);
}

function form_edit_karir($id = 0){ 

	$jabatan = loaddata_jabatan($id) ;
	$fields =  my_get_data_by_id( 'jalur_karir' , 'jabatan_id' , (($id > 0) ?$id: 0 )); 
	$view = form_header( "jabatan link" , "jabatan link"  );
	$view .= form_field_display( ucfirst($jabatan['nomor_posisi']) , 'Nomor Posisi' );	
	$view .= form_field_display( ucfirst($jabatan['nama_jabatan']) , 'Jabatan' );

	$keluarga_jabatans = array();
	$query = "SELECT * FROM keluarga_jabatan ";
	$result = my_query($query);
	while($row = my_fetch_array($result)){
		$keluarga_jabatans[$row['keluarga_jabatan_id']] = $row['keluarga_jabatan_label'];
	}
	$keluarga_jabatan = array(
		'name'=>'keluarga_jabatan_id',
		'value'=>(isset($_POST['keluarga_jabatan_id']) ? $_POST['keluarga_jabatan_id'] : $fields['keluarga_jabatan_id'] ),
		'name'=>'keluarga_jabatan_id',
	);
	$form_field_keluarga_jabatan = form_dropdown($keluarga_jabatan , $keluarga_jabatans);
	$view .= form_field_display( $form_field_keluarga_jabatan  , "Job Family"    );
	
	$submit = array(
		'value' => ( $id ==0 ? ' Lanjut ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit);
	$reset = array(
		'value' => 'Kembali',
		'name' => '', 
		'type'=>'button',
		'onclick'=>'javascript:location.href=\'index.php?com=pengajuan_cuti\'',
		'class'=>'main_button'
	);
	$form_reset= form_dynamic($reset);
	
	$view .= form_field_display( $form_submit   , "&nbsp;" ,  "" );
	$view .= form_footer();
	return $view;
}

function form_submit_data_jalur_karir($id = 0){
	$query = "SELECT jalur_karir_id FROM jalur_karir WHERE jabatan_id = {$id} ";
	$result = my_query($query);
	if(	my_num_rows($result) > 0 ){
		$datas = array(
			'keluarga_jabatan_id'=>my_type_data_int($_POST['keluarga_jabatan_id'])
		);
		return my_update_record( 'jalur_karir' , 'jabatan_id' , $id , $datas);
	}else{
		$datas = array(
			'jabatan_id'=>my_type_data_int($id),
			'keluarga_jabatan_id'=>my_type_data_int($_POST['keluarga_jabatan_id'])
		);
		return my_insert_record( 'jalur_karir' , $datas );
	} 
}

function jalur_karir_detail($id){
	$query = "
		SELECT * FROM  jalur_karir 
		WHERE  jabatan_id = {$id}";  
	$result = my_query($query);
	$datas  = my_fetch_array($result);

	$jabatan = my_get_data_by_id('jabatan','jabatan_id', $id);
	$kjabid=$datas['keluarga_jabatan_id'];
	$kjab = my_get_data_by_id('keluarga_jabatan' ,'keluarga_jabatan_id' , (($kjabid > 0) ?$kjabid: 0 ));
	$view  = form_header( "detail pegawai" , "cp"  );
	$view .= '<tr><td><td><div style="width:100%;text-align:right;">
	<input type="button" value="Auto" class="main_button" onclick="javascript:location.href=\'index.php?com=jalur_karir&task=auto&id='.$id.'\'"/>
	<input type="button" value="Back" class="main_button" onclick="javascript:location.href=\'index.php?com=jalur_karir\'"/>
	</div></td></tr>'   ; 	
	$view .= form_field_display( '<span class="label_form">'.ucfirst($jabatan['nomor_posisi']).'</span>'   , "<b>Nomor Posisi</b>"    ); 
	$view .= form_field_display( '<span class="label_form">'.ucfirst($jabatan['nama_jabatan']).'</span>'   , "<b>Jabatan</b>"    ); 
	$view .= form_field_display( '<span class="label_form">'.$kjab['keluarga_jabatan_label'] .'</span>' , "<b>Job Family</b>"    ); 
	$view .= form_footer( ); 
	
	$tab1 = status_move_list($id ,1);
	$tab2 = status_move_list($id ,3);
	$tab3 = status_move_list($id ,2);
	$tab_options = array('Promosi'=>'tab1', 'Rotasi'=>'tab3' );
	$contents = array('tab1'=>$tab1, 'tab3'=>$tab3   );
	$tabcontents = tab_page($tab_options , $contents );
	return 	$view . $tabcontents;
}

function status_move_list($jabatan_id ,$move_id){
	my_set_code_js('
		function confirmDelete(i){
		var check = confirm(\'Akan mendelete data? \'); 
		if(check){
			location.href=\'index.php?task=delete&com=jalur_karir&id=\'+i;
		}
		return;	
		}
	');	
	$header = array(
		'#'=>array('style'=>'border-bottom:2px solid;width:5%'), 
		'Nomor Posisi'=>array('style'=>'border-bottom:2px solid;width:20%'),  
		'Jabatan'=>array('style'=>'border-bottom:2px solid;width:65%'),  
		 ' '=>array('style'=>'border-bottom:2px solid;width:10%'),     
	);

	$query = "SELECT * FROM jalur_karir_rotasi a
		INNER JOIN jalur_karir_move b
			ON a.jalur_karir_move_id = b.jalur_karir_move_id 
		WHERE a.jabatan_id = {$jabatan_id} 
		AND a.jalur_karir_move_id={$move_id}";	
/*	
	$query = "SELECT * FROM jalur_karir a
		INNER JOIN jalur_karir_rotasi c
			ON a.jabatan_id = c.jabatan_id
		INNER JOIN jalur_karir_move b
			ON c.jalur_karir_move_id = b.jalur_karir_move_id 
		WHERE a.jalur_karir_id = {$jalur_karir_id} 
		AND c.jalur_karir_move_id={$move_id}";
*/	 
	$result = my_query($query); 
	$i = 0;
	$rowx = array(); 
	while($row = my_fetch_array($result) ){
		
		$i++;
		$editproperty = array(
			'href'=>"index.php?com=jalur_karir&task=edit_move_popup
			&id={$row['jalur_karir_rotasi_id']}
			&jid={$jabatan_id}
			&mid={$move_id}", 
			'title'=>'Edit',
			'rel'=>'facebox'
		);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );
		$deleteproperty = array(
						'href'=>'javascript:void();', 
						'title'=>'Edit',
						'onclick'=>'javascript:confirmDelete( '.$row['jalur_karir_rotasi_id'].')'
				);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		$delete_button = admin_link( $delete_button,'&nbsp;');
		
		$newjabatan = my_get_data_by_id( 'jabatan' , 'jabatan_id' , $row['new_jabatan_id'] );
		
		$rowx[] = array(
			'#'=> $i,  
			'nomor'=>$newjabatan['nomor_posisi'],		
			'jabatan'=>$newjabatan['nama_jabatan'] ,   
			'edit'=>position_text_align(   $delete_button,'right')
		);  
	}
	
	if($i < 5){
		for($n = $i; $n<=5; $n++){
			$rowx[] = array(
				'#'=> '&nbsp;'	,
				'nomor'=> '&nbsp;'	, 
				'sd'=> '&nbsp;'	, 
				'op'=> '&nbsp;'	,  
			); 
		}
	}
	$datas = table_rows_plain($rowx); 
	
	$view = table_builder_plain($header , $datas ,  3 ,false );
	$view  .= '<div style="text-align:right;width:100%;font:12px verdana;">'
		.admin_link('<a href="index.php?com=jalur_karir
		&task=edit_move_popup
		&id=0
		&jid='.$jabatan_id.'
		&mid='.$move_id.'" rel="facebox">[Tambah data]</a>').'</div>'; 
	return $view;
}

function form_jalur_karir_move_validate($jid,$mid ){
	$errsubmit = false;
	$err = array();
	$jabatans = my_get_data_by_id('jabatan','jabatan_id',$jid);
	$new_jabatan_id = check_jabatan_by_no_org($_POST["nama_jabatan"]);
	$njabs = my_get_data_by_id('jabatan','jabatan_id',$new_jabatan_id);
	/*CHECK CODE*/
	$nama =  trim($_POST["nama_jabatan"]); 
	
	if( $nama == ''){
		$errsubmit = true;
		$err[] = "Jabatan belum di isi";
	}
	elseif($jid == $new_jabatan_id){
		$errsubmit = true;
		$err[] = "Tidak melakukan pindah jabatan";
	}
	elseif($jabatans['level_id'] < $njabs['level_id'] ){
		$errsubmit = true;
		$err[] = "Jabatan pindah ke posisi lebih rendah (demosi)";
	}
	
	elseif($mid == '1'){
	
		if($jabatans['level_id'] == $njabs['level_id'] ){
			//$errsubmit = true;
			//$err[] = "Jabatan pindah ke posisi setara (mutasi)";
			if($jabatans['grade_id'] == $njabs['grade_id'] ){
				$errsubmit = true;
				$err[] = "Jabatan pindah ke posisi setara (mutasi)";
			}
			elseif( $jabatans['grade_id'] > $njabs['grade_id']){
				$errsubmit = true;
				$err[] = "Jabatan pindah ke posisi turun (demosi)";
			}
		}
		
	}elseif($mid == '2'){
		if($jabatans['level_id'] > $njabs['level_id'] ){
			$errsubmit = true;
			$err[] = "Jabatan pindah ke posisi  naik (promosi)";
		}
		
		elseif($jabatans['grade_id'] > $njabs['grade_id']){
			$errsubmit = true;
			$err[] = "Jabatan pindah ke posisi naik (promosi)";
		}
	}
/*
	$query="SELECT jalur_karir_rotasi_id FROM jalur_karir_rotasi 
		WHERE new_jabatan_id=".$new_jabatan_id.
		" AND jabatan_id=".$jid; 
		" AND jabatan_pendidikan_link_id<>".$id; 
	if (my_num_rows( my_query($query)) >0){
		$errsubmit = true;
		$err[] = "Tingkat Pendidikan sudah digunakan";	
	}
*/	
	if( $errsubmit){
		return $err;
	}
	
	return $errsubmit;
}

function form_jalur_karir_move($id , $jid ,$mid  ){
	my_set_file_js(
		array(
		'components/system/jquery/autocomplete/jquery.autocomplete.js'  
		)
	);
	my_set_file_css(
		array(
			'components/system/jquery/autocomplete/jquery.autocomplete.css' 
		)
	);

	my_set_code_js('  
		function findValue(li) {
			if( li == null ) return alert("No match!"); 
			if( !!li.extra ) var sValue = li.extra[0]; 
			else var sValue = li.selectValue;
		}

	function selectItem(li) {
		findValue(li);
	}

	function formatItem(row) {
		return   row[0] ;
		}
 
	$(document).ready(function() {
		$("#nama_jabatan").autocomplete(
			"autocomplete_daftar_jabatan.php",
			{
				delay:10,
				minChars:2,
				matchSubset:1,
				matchContains:1,
				cacheLength:5,
				onItemSelect:selectItem,
				onFindValue:findValue,
				formatItem:formatItem,
				autoFill:true
			}
		);
	});

	function checkName(name){
		$.get("check_jabatan_names.php", { nama: name  },
		function(data){
			if(data ==  \'0\' ){
				alert(\'Jabatan \'+ name+\' tidak ditemukan\nHarap diperiksa kembali\');
				$(\'#nama_jabatan\').val(\'\');
				return false;
			} 
		});
	}
	');
	$jabatans = my_get_data_by_id('jabatan','jabatan_id',$jid);
	if($id>0){
		$nomor_organisasi=$jabatans['nomor_posisi']."/".$jabatans['nama_jabatan'] ;
		$fields =  my_get_data_by_id( 'jalur_karir_rotasi' , 'jalur_karir_rotasi_id' ,  $id ); 
	}else{
		$nomor_organisasi='/';
	}	
	$keljabs  = my_get_data_by_id('jalur_karir','jabatan_id',$jid); 
	if($keljabs){
		$keljab=$keljabs['keluarga_jabatan_id'];
	}else{
		$keljab=0;
	}
	$keluarga_jabatans  = my_get_data_by_id('keluarga_jabatan','keluarga_jabatan_id',$keljab);  
	$mutasis = my_get_data_by_id( 'jalur_karir_move' , 'jalur_karir_move_id' , $mid );
	$view = form_header( "jabatan link" , "jabatan link"  );
	$view .= form_field_display( $jabatans['nomor_posisi']  , "Nomor Posisi"    );
	$view .= form_field_display( $jabatans['nama_jabatan']  , "Jabatan"    );
	$view .= form_field_display( $keluarga_jabatans['keluarga_jabatan_label']  , "Job Family"    );
	$view .= form_field_display( ucfirst($mutasis['label']  ), "Status mutasi"    );
	
	$newjabatan = my_get_data_by_id('jabatan','jabatan_id',($fields['new_jabatan_id'] ? $fields['new_jabatan_id'] : 0));
	$nama_jabatan = array(
		'name'=>'nama_jabatan',
		'value'=>( isset($_POST['nama_jabatan'] ) ? $_POST['nama_jabatan'] : $nomor_organisasi  ),
		'id'=>'nama_jabatan' , 
		'type'=>'text',
		'size'=>'60' ,
		'onchange'=>'javascript:checkName(this.value)'
	);
	$form_field_nama_jabatan = form_dynamic($nama_jabatan);
	$view .= form_field_display( $form_field_nama_jabatan  , "No. Pos./Jabatan"    );
	 
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit);
 	
	$view .= form_field_display( $form_submit   , "&nbsp;" ,  "" );
	$view .= form_footer();
	return $view;
} 

function   submit_jalur_karir_move($id=0 , $jid ,$mid){
	$jabatan_id = check_jabatan_by_no_org($_POST['nama_jabatan']	);
	$datas = array(
		'jabatan_id'=>my_type_data_int($jid ),		 	 	 	 	 	 	 
		'new_jabatan_id'	=> my_type_data_int($jabatan_id ),		
		'jalur_karir_move_id'=> my_type_data_int($mid),
	);
	if($id > 0){
		return my_update_record('jalur_karir_rotasi','jalur_karir_rotasi_id',$id, $datas);	
	}
	return my_insert_record('jalur_karir_rotasi' , $datas);
}

function delete_jalur_karir($id){
	$query = "DELETE FROM jalur_karir_rotasi WHERE jalur_karir_rotasi_id ={$id} ";
	return my_query($query);
}

function jalur_karir_auto($jabatan_id){
	$query = " SELECT parent_id FROM jabatan 
		WHERE jabatan_id=".$jabatan_id
	;
	$result = my_query($query);	
	$parents = my_fetch_array($result);
	if($parents){
		// ROTASI	
		$query = " SELECT a.jabatan_id FROM jabatan a
			INNER JOIN sk_jabatan b ON b.sk_id=a.sk_id
			WHERE a.parent_id=".$parents['parent_id']."
			AND a.jabatan_id <>".$jabatan_id."
			AND b.active='Y' "
		;
		$result = my_query($query);	
		while($ey = my_fetch_array($result)){
			$query = " SELECT jalur_karir_rotasi_id FROM jalur_karir_rotasi 
				WHERE jabatan_id=".$jabatan_id." 
				AND new_jabatan_id=".$ey['jabatan_id'] ."
				AND jalur_karir_move_id=2" ;
			$dres = my_query($query);
			if(my_num_rows($dres)==0){ ;	
				$datas = array(
					'jabatan_id'=>my_type_data_int($jabatan_id ),		 	 	 	 	 	 	 
					'new_jabatan_id'	=> my_type_data_int($ey['jabatan_id']),		
					'jalur_karir_move_id'=> my_type_data_int(2),
				);				
				my_insert_record('jalur_karir_rotasi' , $datas);
			}
		}
		// PROMOSI
		$query = " SELECT a.parent_id FROM jabatan a
			INNER JOIN sk_jabatan b ON b.sk_id=a.sk_id
			WHERE a.jabatan_id=".$parents['parent_id']." 
			AND b.active='Y' "
		;
		$result = my_query($query);	
		$parent_parents = my_fetch_array($result);
		if($parent_parents){	
			$query = " SELECT a.jabatan_id FROM jabatan a
				INNER JOIN sk_jabatan b ON b.sk_id=a.sk_id
				WHERE a.parent_id=".$parent_parents['parent_id']." 
				AND b.active='Y' "
			;
			$result = my_query($query);			
			while($ey = my_fetch_array($result) ){
				$query = " SELECT jalur_karir_rotasi_id FROM jalur_karir_rotasi 
					WHERE jabatan_id=".$jabatan_id." 
					AND new_jabatan_id=".$ey['jabatan_id'] ."
					AND jalur_karir_move_id=1" ;
				$dres = my_query($query);
				if(my_num_rows($dres)==0){ ;
					$datas = array(
						'jabatan_id'=>my_type_data_int($jabatan_id ),		 	 	 	 	 	 	 
						'new_jabatan_id'	=> my_type_data_int($ey['jabatan_id']),		
						'jalur_karir_move_id'=> my_type_data_int(1),
					);				
					my_insert_record('jalur_karir_rotasi' , $datas);
				}
			}
		}	
	}
	return true ;
}		